/**
 * 
 */
package com.srini.util;

/**
 * @author Sriniz
 * 
 */

public class NumberToWords {

	private String[][] L = {
			{ "", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
					"Eight", "Nine" },
			{ "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
					"Sixteen", "Seventeen", "Eighteen", "Nineteen" },
			{ "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
					"Eighty", "Ninety" }, { "Hundred" }, { "Thousand" },
			{ "Lakh" }, { "Crore" } };

	private final int[] UNITS = { 1, 10, 100, 1000, 100000, 10000000 };

	private final int TEN = 10;

	private final int TWENTY = 20;

	private StringBuilder numInWords = new StringBuilder();

	public String numberToWords(int number) {
		String num = String.valueOf(number);
		int divider, quotient, reminder, level, noOfDigits = num.length();

		switch (noOfDigits) {
		case 1:
		case 2:
		case 3:
			level = noOfDigits;
			break;
		case 4:
		case 5:
			level = 4;
			break;
		case 6:
		case 7:
			level = 5;
			break;
		default:
			level = 6;
			break;
		}
		divider = UNITS[level - 1];

		if (number < TEN) {
			addToString(L[0][number]);
		} else if (number < TWENTY) {
			addToString(L[level - 1][number - 10]);
		} else {
			quotient = number / divider;
			if (quotient > 9) {
				numberToWords(quotient);
				quotient = 0;
			} else if (level > 2) {
				addToString(L[0][quotient]);
				quotient = 0;
			}

			addToString(L[level][quotient]);

			reminder = number % divider;
			numberToWords(reminder);
		}
		return numInWords.toString();
	}

	private void addToString(String str) {
		if (numInWords.length() > 0) {
			numInWords.append(" ");
		}
		numInWords.append(str);
	}

	public static String getInWords(int number) {
		return new NumberToWords().numberToWords(number);
	}

	public static void main(String[] args) {
		System.out.println(getInWords(109756));
	}
}
